package my.jwds.p202;

import java.util.HashSet;

public class IsHappy {

    public static void main(String[] args) {
        IsHappy o = new IsHappy();
        System.out.println(o.isHappy(19));
    }



    public boolean isHappy(int n) {
        return isHappy(n,new HashSet());
    }



    public boolean isHappy(long n, HashSet cache){
        if (cache.contains(n))return false;
        cache.add(n);
        long sum = 0;
        while(n != 0){
            long temp = n%10;
            sum += temp * temp;
            n /= 10;
        }
        if (sum == 1)return true;
        return isHappy(sum,cache);
    }
}
